package com.luojie.controller.user;

import com.luojie.dto.OrderSubmitDto;
import com.luojie.dto.OrdersPaymentDTO;
import com.luojie.result.Result;
import com.luojie.service.OrdersService;
import com.luojie.vo.OrderPaymentVO;
import com.luojie.vo.OrderSubmitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roger/user/orders")
@Api(tags = "c端用户相关接口")
@Slf4j
public class OrdersController  {
    @Autowired
    private OrdersService ordersService;
    @PostMapping("submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVo> submit (@RequestBody OrderSubmitDto orderSubmitDto){
        try {
            log.info("用户下单：{}", orderSubmitDto);
            OrderSubmitVo orderSubmitVo= ordersService.submitOrder(orderSubmitDto);
            return Result.success(orderSubmitVo);
        } catch (Exception e) {
            throw new RuntimeException(e);

        }
    }
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO)throws Exception {
            log.info("订单支付：{}", ordersPaymentDTO);
            OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
            log.info("生成预支付交易单：{}", orderPaymentVO);
            return Result.success(orderPaymentVO);

    }
    /**
     * 用户取消订单
     *
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable("id") Long id,OrderSubmitDto orderSubmitDto) throws Exception {
        ordersService.userCancelById(id,orderSubmitDto);
        return Result.success();
    }



}
